
/****************************************************************************
*						CSocketCommunication Class header					*
*    				Copyright (C) 2012  J.R. Ruiz-Sarmiento					*
*																			*
*    This program is free software: you can redistribute it and/or modify	*
*    it under the terms of the GNU General Public License as published by	*
*    the Free Software Foundation, either version 3 of the License, or		*
*    (at your option) any later version.									*
*																			*
*    This program is distributed in the hope that it will be useful,		*
*    but WITHOUT ANY WARRANTY; without even the implied warranty of			*
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			*
*    GNU General Public License for more details.							*
*																			*
*    You should have received a copy of the GNU General Public License		*
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.	*
*																			*
****************************************************************************/

#ifndef _SORT_SOCKETCOMMUNICATION_
#define _SORT_SOCKETCOMMUNICATION_

#include <string>
#include <vector>

#include <mrpt/utils/CServerTCPSocket.h>
#include <mrpt/utils/CClientTCPSocket.h>

using namespace mrpt::utils;

namespace SORT
{
namespace DataIO
{
	/** Communication class. This clas open a TCP Server attached to a socket for
	  *		sending a retrieving data.
	  */
	class CSocketCommunication 
	{
	private:

		std::string		m_ip;	//!< IP direction where we are going to open the port 
		unsigned short	m_port; //!< Port to open

		CServerTCPSocket *srvSocket; //!< Server
		CClientTCPSocket *socket;	//!< Client

	public:

		/** Constructor */
		CSocketCommunication();

		/** Destructor */
		~CSocketCommunication();

		/** Module initialization reading configuration parameters from file. 
		  * \return true if the initialization was successful, false in otherwise.	
		  */
		bool init();

		/** Wait for a connection in the specified ip and port.
		  * \return true if the client connection was successful, false in otherwise.	
		  */
		bool run();

		/** Write a string in the socket.
		  * \param str string to write.
		  * \return true if any error occurs.
		  */
		bool write( const std::string &str );

		/** Read a string from the socket.
		  * \param read string readed.
		  * \return true if any error occurs.
		  */
		bool read(std::string &read);

		/** Read various lines from the socket.
		  * \param read vector with the lines read as strings.
		  * \param n_lines number of lines to read.
		  * \return true if any error occurs.
		  */
		bool readVariousLines(std::vector<std::string> &read, const size_t &n_lines);

	};
}
}

#endif